<template>
  <div>
   <el-card>
      <div slot="header">
        <my-breadcrumb>评论管理</my-breadcrumb>
      </div>

      <!-- 表格 -->
      <el-table
        :data="comments">
        <el-table-column label="文章标题" prop="title">
        </el-table-column>
         <el-table-column width="100px" label="总评论数" prop="total_comment-count">
        </el-table-column>
         <el-table-column width="100px" label="粉丝评论数" prop="fans_comment-count">
        </el-table-column>
         <el-table-column width="100px" label="评论状态">
           <template slot-scope="scope">
             <!-- {{scope.row.comment_status}} -->
             <el-tag v-if="scope.row.comment_status">正常</el-tag>
             <el-tag v-else type="warning">关闭</el-tag>
           </template>
        </el-table-column>
         <el-table-column width="100px" label="操作" prop="">
           <template slot-scope="scope">
             <!-- 作用域插槽  scope.row保存当前行的数据-->
             <el-button size="mini" v-if="scope.row.comment_status" @click="hSwitch(scope.row)">打开评论</el-button>
             <el-button size="mini" v-else type="danger"  @click="hSwitch(scope.row)">关闭评论</el-button>
           </template>
        </el-table-column>
      </el-table>

      <!-- 分页 -->
      <el-pagination
        background
        @current-change="hCurrentChange"
        layout="prev, pager, next"
        :total="total_count">
      </el-pagination>
    </el-card>
  </div>
</template>

<script>
export default {
  name: 'Comment',
  data () {
    return {
      total_count: 0, // 总条数
      comments: []
    }
  },
  created () {
    this.loadComments()
  },
  methods: {
    async doSwitch (row) {
      try {
        const res = await this.$axios({
          method: 'put',
          url: '/mp/v1_0/comments/status',
          params: {
            acticle_id: row.id.toString()
          },
          data: {
            allow_comment: !row.comment_status
          }
        })
        console.log(res)
        // 更新页面
        // 直接在本地更新
        row.comment_status = !row.comment_status
        this.$message.success('操作成功')
      } catch (err) {
        console.log(err)
        this.$message.error('操作失败')
      }
    },
    // 用户打开或关闭评论
    hSwitch (row) {
      if (row.comment_status) {
        this.$confirm('你确定要关闭吗？关了就无法评论了').then(() => {
          this.doSwitch(row)
        }).catch(() => {

        })
      } else {
        this.doSwitch(row)
      }
    },
    hCurrentChange (curPage) {
      // 1.更新页码
      this.page = curPage
      // 2.重发请求
      this.loadComments()
    },
    builParams () {
      const params = {
        page: this.page,
        response_type: 'comment'
      }
      return params
    },
    async  loadComments () {
      // 获取条件
      const params = this.builParams()
      try {
        const res = await this.$axios({
          method: 'get',
          url: '/mp/v1_0/articles',
          params
        })
        console.log(res)
        this.comments = res.data.data.results
        this.total_count = res.data.data.total_count
      } catch (err) {
        console.log(err)
      }
    }
  }
}
</script>

<style scoped lang='less'></style>
